package com.global.emc.spring.web.servlet.mvc.method.annotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;

import com.global.emc.core.exception.IEMCException;
import com.global.emc.spring.exception.handler.ConvertEMCExceptionHandlerManager;
import com.global.emc.spring.exception.handler.EMCExceptionHandlerManager;
import com.global.emc.spring.exception.handler.EMCExceptionMessagesHandlerManager;
import com.global.emc.spring.exception.handler.ModelAndViewEMCExceptionHandlerManager;

/**
 * 
 * @Description: [处理异常]
 * @Author: [Joly]
 * @CreateDate: [2013年12月16日 下午1:33:02]
 * @UpdateUser: [Joly]
 * @UpdateDate: [2013年12月16日 下午1:33:02]
 * @UpdateRemark: [说明本次修改内容]
 * @Version: [v1.0]
 * 
 */
public class ExtendedHandlerExceptionResolver extends AbstractHandlerExceptionResolver {
	
	private ConvertEMCExceptionHandlerManager convertEMCExceptionHandlerManager;
	private EMCExceptionMessagesHandlerManager emcExceptionMessagesHandlerManager;
	private ModelAndViewEMCExceptionHandlerManager modelAndViewEMCExceptionHandlerManager;
	private EMCExceptionHandlerManager emcExceptionHandlerManager;
	

	public ConvertEMCExceptionHandlerManager getConvertEMCExceptionHandlerManager() {
		return convertEMCExceptionHandlerManager;
	}
	public void setConvertEMCExceptionHandlerManager(ConvertEMCExceptionHandlerManager convertEMCExceptionHandlerManager) {
		this.convertEMCExceptionHandlerManager = convertEMCExceptionHandlerManager;
	}
	public EMCExceptionMessagesHandlerManager getEmcExceptionMessagesHandlerManager() {
		return emcExceptionMessagesHandlerManager;
	}
	public void setEmcExceptionMessagesHandlerManager(EMCExceptionMessagesHandlerManager emcExceptionMessagesHandlerManager) {
		this.emcExceptionMessagesHandlerManager = emcExceptionMessagesHandlerManager;
	}
	public ModelAndViewEMCExceptionHandlerManager getModelAndViewEMCExceptionHandlerManager() {
		return modelAndViewEMCExceptionHandlerManager;
	}
	public void setModelAndViewEMCExceptionHandlerManager(ModelAndViewEMCExceptionHandlerManager modelAndViewEMCExceptionHandlerManager) {
		this.modelAndViewEMCExceptionHandlerManager = modelAndViewEMCExceptionHandlerManager;
	}
	public EMCExceptionHandlerManager getEmcExceptionHandlerManager() {
		return emcExceptionHandlerManager;
	}
	public void setEmcExceptionHandlerManager(EMCExceptionHandlerManager emcExceptionHandlerManager) {
		this.emcExceptionHandlerManager = emcExceptionHandlerManager;
	}

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {

		super.prepareResponse(exception, response);

		return doResolveException(request, response, handler, exception);
	}

	@Override
	protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handlerMethod, Exception exception) {

		if (this.logger.isDebugEnabled())
			this.logger.error(exception);

		IEMCException emcException = convertEMCExceptionHandlerManager.convertException(request, response, handlerMethod, exception);
		emcExceptionMessagesHandlerManager.handlerExceptionMessages(request, response, handlerMethod, emcException);
		ModelAndView modelAndView =modelAndViewEMCExceptionHandlerManager.handlerModelAndView(request, response, handlerMethod, emcException);
		emcExceptionHandlerManager.handlerException(request, response, handlerMethod, emcException, modelAndView);
		
		return modelAndView;
	}

}
